package com.example.a623

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_login.*
import okhttp3.*
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import org.json.JSONObject


class Login : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        var sp=getSharedPreferences("data", MODE_PRIVATE)
        var handler=Handler(Looper.myLooper()!!)

        if(sp.getString("ischecked","false").equals("true"))
        {edit_userName.setText(sp.getString("user_name",""))
        edit_password.setText(sp.getString("password",""))
        checkbox.isChecked=true}

        internet_btn.setOnClickListener {
            startActivity(Intent(applicationContext,Internet::class.java))
        }
        login_btn.setOnClickListener {
            if (sp.getString("adress","").equals("")||sp.getString("port","").equals(""))
            {Toast.makeText(applicationContext,"请先设置网络！",Toast.LENGTH_SHORT).show()}
            else if (sp.getString("adress","").equals("192.168.100.230")&&sp.getString("port","").equals("10001"))
            {
                Thread{
                    if (edit_userName.text.toString().equals("")||edit_password.text.toString().equals(""))
                    {handler.post { Toast.makeText(applicationContext,"请输入用户名密码！",Toast.LENGTH_SHORT).show() }}
                    else {
                        val client: OkHttpClient = OkHttpClient().newBuilder()
                            .build()
                        val mediaType: MediaType? = "application/json".toMediaTypeOrNull()
                        val body: RequestBody = RequestBody.create(
                            mediaType,
                            "{ \"username\":\""+edit_userName.text.toString()+"\", \"password\":\""+edit_password.text.toString()+"\" }"
                        )
                        val request: Request = Request.Builder()
                            .url("http://"+sp.getString("adress","")+":"+sp.getString("port","")+"/prod-api/api/login")
                            .method("POST", body)
                            .addHeader("Content-Type", "application/json")
                            .build()
                        val response: Response = client.newCall(request).execute()

                        var data = JSONObject(response.body?.string())
                        var code = data.getString("code")
                        if (code.equals("200")) {
                            startActivity(Intent(applicationContext, Home::class.java))
                        }
                        else if(code.equals("500"))
                        {handler.post { Toast.makeText(applicationContext,"用户名密码错误！",Toast.LENGTH_SHORT).show() }}
                    }
                }.start()
            }
            else{Toast.makeText(applicationContext,"请输入正确的服务器地址与端口号！",Toast.LENGTH_SHORT).show()}
            if (checkbox.isChecked){
                var editor=sp.edit()
                editor.putString("user_name",edit_userName.text.toString())
                editor.putString("password",edit_password.text.toString())
                editor.putString("ischecked","true")
                editor.commit()
            }
        }
    }
}